package cn.wolfcode.luowowo.mgrsite.util;

import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.article.vo.TravelStatisPersistenceVO;
import cn.wolfcode.luowowo.cache.domain.TravelStatisVO;
import cn.wolfcode.luowowo.cache.service.ITravelStatisRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * redis数据持久化
 */
@Component
public class TravelRedisDataPersistenceJob {

    @Reference
    private ITravelStatisRedisService statisRedisService;

    @Reference
    private ITravelService detailService;

    @Scheduled(cron = "0 0 0,13 1/1 * ?")
    public void redisToMysql(){
        System.out.println("---------开始进行游记的Redis数据落地------------");

        //获取需要持久化的数据  此处:攻略统计数据(即阅读量,收藏量等数据)
        //即取出所有前缀为之前设计好了的travel_statis_vo的key里面的统计数据值
        List<TravelStatisVO> list = statisRedisService.getTravelStatisVOs(RedisKeys.TRAVEL_STATIS_VO.getPrefix());
        for (TravelStatisVO vo : list) {
            TravelStatisPersistenceVO pvo = new TravelStatisPersistenceVO();
            try {
                BeanUtils.copyProperties(pvo,vo);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            detailService.updateStatisVO(pvo);
        }
        System.out.println("---------游记的Redis数据落地结束------------");
    }
}
